package cn.lili.modules.payment.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.lili.modules.order.order.client.OrderClient;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.OrderFlow;
import cn.lili.modules.payment.service.OutOrderLogService;
import cn.lili.modules.payment.service.WechatBillService;
import cn.lili.modules.payment.service.WithdrawLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class PaymentEveryDayTask {

    /**
     * 分账日志-分账业务
     */
    private final OutOrderLogService outOrderLogService;
    /**
     * 商户提现
     */
    private final WithdrawLogService withdrawLogService;
    /**
     * 微信账单
     */
    private final WechatBillService wechatBillService;


    /**
     * 执行每日任务
     */
    public void execute() {
        //查询分账状态
        try {
            outOrderLogService.queryProfitSharing();
        } catch (Exception e) {
            log.error("查询分账状态", e);
        }
        //提现
        try {
            withdrawLogService.subWithdraw();
        } catch (Exception e) {
            log.error("提现", e);
        }
        //查询提现状态
        try {
            withdrawLogService.querySubWithdraw();
        } catch (Exception e) {
            log.error("查询提现状态", e);
        }
    }

    /**
     * 获取微信账单
     */
    public void wechatBill() {
        //每天10点获取 获取前1天的交易账单
        //每天10点获取 获取前1天的资金账单
        //每天10点获取 获取前1天的分账账单
        if (10 == DateUtil.hour(new DateTime(), true)) {
            //申请交易账单
            try {
                wechatBillService.tradebill(new DateTime());
            } catch (Exception e) {
                log.error("申请交易账单", e);
            }
            //申请资金账单
            try {
                wechatBillService.fundFlowBill(new DateTime());
            } catch (Exception e) {
                log.error("申请资金账单", e);
            }
            //申请分账账单
            try {
                wechatBillService.bills(new DateTime());
            } catch (Exception e) {
                log.error("申请分账账单", e);
            }
        }

    }

}
